package com.tanhua.server.controller;

import com.tanhua.model.vo.MovementVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.MovementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 圈子控制层
 */
@RestController
@RequestMapping("/movements")
@Slf4j
public class MovementController {

    @Autowired
    private MovementService movementService;

    /**
     *  动态-发布
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity addMovement(MovementVo movementVo, MultipartFile[] imageContent){
        movementService.addMovement(movementVo,imageContent);
        return ResponseEntity.ok(null);
    }


    /**
     * 我的动态
     * userId:为了当前此功能给其它地方使用
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public ResponseEntity findPageMovement(@RequestParam(value = "page",defaultValue = "1") Long page, @RequestParam(value = "pagesize",defaultValue = "10") Long pagesize,Long userId){
        PageResult<MovementsVo> pageResult = movementService.findPageMovement(page,pagesize,userId);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 好友动态
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity findPageMovementByFriend(@RequestParam(value = "page",defaultValue = "1") Long page, @RequestParam(value = "pagesize",defaultValue = "10") Long pagesize){
        PageResult<MovementsVo> pageResult = movementService.findPageMovementByFriend(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐动态
     */
    @RequestMapping(value = "/recommend",method = RequestMethod.GET)
    public ResponseEntity findPageMovementByRecommend(@RequestParam(value = "page",defaultValue = "1") Long page, @RequestParam(value = "pagesize",defaultValue = "10") Long pagesize){
        PageResult<MovementsVo> pageResult = movementService.findPageMovementByRecommend(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 单条动态
     * publishId:动态表主键id
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResponseEntity findMovement(@PathVariable("id") String publishId){
        MovementsVo movementsVo = movementService.findMovement(publishId);
        log.debug("************单条动态*************"+movementsVo);
        return ResponseEntity.ok(movementsVo);
    }

    /**
     * 动态点赞
     */
    @RequestMapping(value = "/{id}/like",method = RequestMethod.GET)
    public ResponseEntity like(@PathVariable("id") String publishId){
        Integer count = movementService.like(publishId);
        log.debug("************动态点赞{}*************",count);
        return ResponseEntity.ok(count);
    }

    /**
     * 动态取消点赞
     */
    @RequestMapping(value = "/{id}/dislike",method = RequestMethod.GET)
    public ResponseEntity dislike(@PathVariable("id") String publishId){
        Integer count = movementService.dislike(publishId);
        log.debug("************动态取消点赞{}*************",count);
        return ResponseEntity.ok(count);
    }



    /**
     * 谁看过我
     * 1.redis 看看当前登录用户是否读取过消息
     * 2.visitors 查询最近的访客记录 （有可能用到redis中时间）
     * 3.tb_userInfo visitors 构造vo
     */
    @RequestMapping(value = "/visitors",method = RequestMethod.GET)
    public ResponseEntity findVisitors(){
        List<VisitorsVo> voList = movementService.findVisitors();
        return ResponseEntity.ok(voList);
    }
}
